package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.anno.Log;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpLogMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.EmpService;
import com.itheima.until.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiceimpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
    @Autowired
    private EmpLogMapper empLogMapper;

//    @Override
//    public PageBean page(Integer page, Integer pageSize) {
////        //获取总记录数
////        Long count = empMapper.selectTotal();
////        //计算起始索引
////        int start = (page - 1) * pageSize;
////        List<Emp> list = empMapper.selectPage(start, pageSize);
////        return  new PageBean(count,list);
//        //开启分页
//        PageHelper.startPage(page,pageSize);
//        //查询当前页数据
//        List<Emp> rows = empMapper.selectPage();
//        //获取总记录数
//        Page<Emp> p = (Page<Emp>) rows;
//        //
//        return new PageBean(p.getTotal(),p.getResult());
//
//    }

    @Override
    public PageBean page(EmpQueryParam empQueryParam) {
        PageHelper.startPage(empQueryParam.getPage(),empQueryParam.getPageSize());
        List<Emp> rows = empMapper.selectPage(empQueryParam);
        Page<Emp> p = (Page<Emp>) rows;
        return  new PageBean(p.getTotal(),p.getResult());
    }
    @Log
    @Override
    @Transactional
    public void add(Emp emp) {
        try {
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());
            empMapper.add(emp);
            List<EmpExpr> exprList = emp.getExprList();
            if (exprList != null&& exprList.size()>0) {
                for (EmpExpr empExpr : exprList) {
                    empExpr.setEmpId(emp.getId());
                }
                empExprMapper.addExpr(exprList);
            }
//            System.out.println(1/0);
        } finally {
            EmpLog empLog = new EmpLog(null, LocalDateTime.now(), "添加员工"+emp.toString());
            empLogMapper.insert(empLog);

        }
    }

    @Override
    public List<Emp> findAll() {
        List<Emp> empList = empMapper.findAll();
        return  empList;
    }
    @Log
    @Override
    @Transactional
    public void delete(List<Integer> ids) {

        //1: 批量  删除员工基本信息
        empMapper.deleteByIds(ids);

        //2:批量 删除员工工作经历
        empExprMapper.deleteByEmpIds(ids);
    }


    @Override
    public Emp getInfoById(Integer id) {
        Emp emp = empMapper.getById(id);
        Integer empId = emp.getId();
        List<EmpExpr> exprList = empExprMapper.getByEmpId(empId);
        //3: 将查询到工作经历设置到员工基本信息中
        emp.setExprList(exprList);
        return emp;
    }
    @Log
    @Transactional
    @Override
    public void update(Emp emp) {
        //1:根据员工id修改 员工基本信息
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);//修改基本信息

        Integer empId = emp.getId();
        //2:根据员工id 删除 员工工作经历
//        ArrayList<Integer> empIds = new ArrayList<>();
//        empIds.add(emp.getId());
//        empExprMapper.deleteByEmpIds(empIds);
        empExprMapper.deleteByEmpIds(Arrays.asList(empId));
        //3:重新添加员工工作经历
        List<EmpExpr> exprList = emp.getExprList();
        if(exprList!=null && exprList.size()>0){//如果有工作经历且工作经历大于0条
            //再往数据库添加
            for (EmpExpr empExpr : exprList) {
                // 员工工作经历 补充属性
                empExpr.setEmpId(emp.getId());//员工经历必然属于某个员工
            }
            empExprMapper.addExpr(exprList);
        }
    }

    @Override
    public EmpLoginInfo login(Emp emp) {
        Emp empLoginInfo = empMapper.getUsernameandPassword(emp);
        if (empLoginInfo!=null){
            Map<String,Object> claims =new HashMap<>();
            claims.put("id",empLoginInfo.getId());
            claims.put("username",empLoginInfo.getUsername());
            String token = JwtUtils.generateJwt(claims);
            EmpLoginInfo empLoginInfo1 = new EmpLoginInfo(empLoginInfo.getId(), empLoginInfo.getUsername(), empLoginInfo.getPassword(), token);
            return empLoginInfo1;
        }
        return null;
    }

}
